//
//  WidgetsViewController.swift
//  TimMusic
//
//  Created by nkl on 2024/10/11.
//

import Foundation
import UIKit

class WidgetsViewController: BasicBGViewController, UIScrollViewDelegate {
    lazy var topView: TopBarView = TopBarView(imgName: "ic_widget_top", types: [.menu])

    lazy var segmentControl: UISegmentedControl = {
        let segmentedControl = UISegmentedControl(items: ["Widget", "StandBy"])
        segmentedControl.selectedSegmentIndex = 0
        segmentedControl.backgroundColor = .white.withAlphaComponent(0.1)
        // 设置未选中状态下的文字颜色
        let normalTextAttributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: UIColor.white.withAlphaComponent(0.4),
        ]
        segmentedControl.setTitleTextAttributes(normalTextAttributes, for: .normal)

        // 设置选中状态下的文字颜色
        let selectedTextAttributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: UIColor.black,
        ]
        segmentedControl.setTitleTextAttributes(selectedTextAttributes, for: .selected)

        segmentedControl.addTarget(self, action: #selector(didSelectedItemIndex(sender:)), for: .valueChanged)
        return segmentedControl
    }()

    lazy var pageView: UIScrollView = {
        let mView = UIScrollView()
        mView.backgroundColor = .clear
        mView.contentSize.width = UIScreen.kScreenWidth * 2
        mView.isPagingEnabled = true
        mView.delegate = self
        mView.showsVerticalScrollIndicator = false
        mView.showsHorizontalScrollIndicator = false
        return mView
    }()

    lazy var widgetVc = WidgetsSubViewController()
    lazy var standbyVc = StandBySubViewController()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @objc func didSelectedItemIndex(sender: UISegmentedControl) {
        selectIndex(idx: sender.selectedSegmentIndex)
    }

    func selectIndex(idx: Int) {
        let offsetX = UIScreen.kScreenWidth * CGFloat(idx)
        pageView.setContentOffset(.init(x: offsetX, y: 0), animated: true)
    }

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let idx = Int(scrollView.contentOffset.x / UIScreen.kScreenWidth)
        segmentControl.selectedSegmentIndex = idx
    }

    override func addChildren() {
        super.addChildren()
        view.addSubview(topView)
        view.addSubview(segmentControl)
        view.addSubview(pageView)
        pageView.addSubview(widgetVc.view)
        pageView.addSubview(standbyVc.view)
        addChild(widgetVc)
        addChild(standbyVc)
        topView.menuButton.addTarget(self, action: #selector(showMyWidgetViewController), for: .touchUpInside)
    }

    @objc func showMyWidgetViewController() {
        let vc = MyWidgetListViewController()
        navigationController?.pushViewController(vc, animated: true)
    }

    override func makeConstraints() {
        super.makeConstraints()
        topView.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(44)
        }

        segmentControl.snp.makeConstraints { make in
            make.top.equalTo(topView.snp.bottom).offset(16)
            make.horizontalEdges.equalToSuperview().inset(38)
            make.height.equalTo(28)
        }

        pageView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-tabbarHeight - 40)
            make.top.equalTo(segmentControl.snp.bottom).offset(26)
        }

        widgetVc.view.snp.makeConstraints { make in
            make.leading.top.equalToSuperview()
            make.width.equalTo(UIScreen.kScreenWidth)
            make.height.equalTo(pageView)
        }

        standbyVc.view.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.leading.equalTo(widgetVc.view.snp.trailing)
            make.width.equalTo(UIScreen.kScreenWidth)
            make.height.equalTo(pageView)
        }
    }
}
